package com.youhome.service.service.impl;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.listhub.Disclaimer;
import com.youhome.service.listhub.ListingItemType;
import com.youhome.service.listhub.SimpleOfficeType;
import com.youhome.service.listhub.SimpleParticipantType;
import com.youhome.service.model.ListHub;
import com.youhome.service.response.BrokerageResult;
import com.youhome.service.service.BrokerageService;
import com.youhome.service.service.DownloadService;
import com.youhome.service.service.ListHubService;
import net.sf.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * Created by chen365 on 2019/02/20.
 * 经纪人
 */
@Service
public class BrokerageServiceImpl implements BrokerageService {
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private DownloadService downloadService;

    @Override
    public BrokerageResult selectByHouseId(String houseId) throws CustomException {
        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        if (listHub != null) {
            if (listHub.getDataSource().equalsIgnoreCase("listhub")) {
                String listingKey = listHub.getListingKey();
                if (!StringUtils.isEmpty(listingKey)) {
                    String key = "json/" + listingKey + ".json";
                    System.err.println("key:" + key);
                    try {
                        String json = downloadService.downloadJsonFromOSS(key);
                        if (!StringUtils.isEmpty(json)) {
                            ObjectMapper mapper = new ObjectMapper();
                            ListingItemType listingItemType = mapper.readValue(json, ListingItemType.class);
                            //人名 = participant.firstName + lastName
                            //电话 = 应该是经纪人的电话 participant.primaryContactPhone
                            //公司名字 = office.name
                            //执照号码 = 应该是经纪人的号码 participant.participantId
                            // 声明 disclaimer.value
                            BrokerageResult brokerageResult = new BrokerageResult();
                            brokerageResult.setListing_key(listingKey);
                            ListingItemType.ListingParticipants listingParticipants = listingItemType.getListingParticipants();
                            if (listingParticipants != null) {
                                List<SimpleParticipantType> participant = listingParticipants.getParticipant();
                                if (participant != null && participant.size() > 0) {
                                    SimpleParticipantType simpleParticipantType = participant.get(0);
                                    if (simpleParticipantType != null) {
                                        //人名
                                        String broker_name = simpleParticipantType.getFirstName() + simpleParticipantType.getLastName();
                                        brokerageResult.setBroker_name(broker_name);
                                        //执照号码
                                        String participantId = simpleParticipantType.getParticipantId();
                                        brokerageResult.setLicense_number(participantId);
                                        //电话
                                        String broker_phone = simpleParticipantType.getPrimaryContactPhone();
                                        brokerageResult.setBroker_phone(broker_phone);
                                    }
                                }
                            }
                            //公司名字
                            ListingItemType.Offices offices = listingItemType.getOffices();
                            if (offices != null) {
                                List<SimpleOfficeType> office = offices.getOffice();
                                if (office != null && office.size() > 0) {
                                    SimpleOfficeType simpleOfficeType = office.get(0);
                                    if (simpleOfficeType != null) {
                                        String company_name = simpleOfficeType.getName();
                                        brokerageResult.setCompany_name(company_name);

                                        String officeEmail = simpleOfficeType.getOfficeEmail();
                                        brokerageResult.setOfficeEmail(officeEmail);
                                    }
                                }
                            }
                            //声明
                            Disclaimer disclaimer = listingItemType.getDisclaimer();
                            if (disclaimer != null) {
                                brokerageResult.setDisclaimer(disclaimer.getValue());
                            }
                            return brokerageResult;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (JsonParseException e) {
                        e.printStackTrace();
                    } catch (JsonMappingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}
